﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

namespace produccion.clases
{
    public class CLSPRCPRD : INotifyPropertyChanged
    {
        MDOgeneral mod = new MDOgeneral();
        string codprc_ppr, nomprc_ppr;
        Double valor_ppr;

        public string CODPRC_PPR
        {
            get { return codprc_ppr; }
            set { codprc_ppr = value; }
        }

        public string NOMPRC_PPR
        {
            get { return nomprc_ppr; }
            set { nomprc_ppr = value; }
        }

        public double VALOR_PPR
        {
            get { return valor_ppr; }
            set { valor_ppr = value; }
        }

        
        public void LimpiarValores()
        {
            CODPRC_PPR = "";
            NOMPRC_PPR = "";
            VALOR_PPR = 0;
        }

        public Boolean ExisteProceso()
        {
            try
            {
                string query = "select nomprc_ppr from prcprd where codprc_ppr='" + CODPRC_PPR + "'";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    NOMPRC_PPR = dt.Rows[0][0].ToString();
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ConsultarTodas()
        {
            try
            {
                string query = "select codprc_ppr as Codigo, nomprc_ppr as Nombre, valor_ppr as Valor from prcprd order by codprc_ppr, nomprc_ppr";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count.Equals(0))
                {
                    query = "select '' as codprc_ppr, '' as nomprc_ppr, 0 as valor_ppr from prcprd";
                    dt = mod.consulta(query);
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable Consultar()
        {
            try
            {
                string query = "select codprc_ppr, nomprc_ppr from prcprd where codprc_ppr='" + CODPRC_PPR + "'";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    CODPRC_PPR = dt.Rows[0][0].ToString();
                    NOMPRC_PPR = dt.Rows[0][1].ToString();
                    VALOR_PPR = Convert.ToDouble(dt.Rows[0][2].ToString());
                }
                else
                {
                    CODPRC_PPR = "";
                    NOMPRC_PPR = "";
                    VALOR_PPR = 0;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<CLSPRCPRD> listarProcesos()
        {
            try
            {
                string query = "select codprc_ppr, nomprc_ppr, valor_ppr from prcprd where estado_ppr = 'A' order by nomprc_ppr ";
                DataTable dt = mod.consulta(query);
                List<CLSPRCPRD> lista=new List<CLSPRCPRD>();

                foreach (DataRow r in dt.Rows) 
                {
                    lista.Add(new CLSPRCPRD 
                    { 
                        CODPRC_PPR=r["codprc_ppr"].ToString(),
                        NOMPRC_PPR=r["nomprc_ppr"].ToString(),
                        VALOR_PPR=Convert.ToDouble(r["valor_ppr"].ToString())
                    });
                
                }
             
                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<CLSPRCPRD> listarProcesosDevolucion()
        {
            try
            {
                string query = "select codprc_ppr, nomprc_ppr, valor_ppr from prcprd where estado_ppr = 'A' and aceptadev_ppr='S' order by nomprc_ppr ";
                DataTable dt = mod.consulta(query);
                List<CLSPRCPRD> lista = new List<CLSPRCPRD>();

                foreach (DataRow r in dt.Rows)
                {
                    lista.Add(new CLSPRCPRD
                    {
                        CODPRC_PPR = r["codprc_ppr"].ToString(),
                        NOMPRC_PPR = r["nomprc_ppr"].ToString(),
                        VALOR_PPR = Convert.ToDouble(r["valor_ppr"].ToString())
                    });

                }

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from prcprd where codprc_ppr='" + CODPRC_PPR + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update prcprd set nomprc_ppr='" + NOMPRC_PPR + "',valor_ppr=" + VALOR_PPR + " where codprc_ppr='" + CODPRC_PPR + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "insert into prcprd (codprc_ppr, nomprc_ppr, valor_ppr) values ('" + CODPRC_PPR + "','" + NOMPRC_PPR + "'," + VALOR_PPR + ")";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable Buscar()
        {
            try
            {
                string query = "select codprc_ppr as codigo, nomprc_ppr as nombre from prcprd where nomprc_ppr like '%" + NOMPRC_PPR + "%' order by nomprc_ppr asc";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
